package com.mofofa.maozy.components.bill

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import androidx.fragment.app.FragmentManager
import com.mofofa.maozy.R
import com.mofofa.maozy.base.BaseFormAction
import com.mofofa.maozy.databinding.ViewMaozyBillBankAccountBinding

/**
<declare-styleable name="MaozyBillBankAccount">
<attr name="cwbbaAddMode" format="boolean" />
<attr name="cwbbaAccountTitle" format="string" />
<attr name="cwbbaAddTitle" format="string" />
<attr name="cwbbaIsSelect" format="boolean" />
</declare-styleable>
 * */
class BankAccountBill : RelativeLayout, BaseFormAction {

    lateinit var mMaozyViewMaozyBinding: ViewMaozyBillBankAccountBinding
    private lateinit var vMaozyRootMaozyView: View

    private var cwbbaAddMode: Boolean = false
    private var cwbbaIsSelect: Boolean = false
    private var cwbbaAccountTitle: String? = null
    private var cwbbaAddTitle: String? = null

    private var mMaozySupportMaozyFragmentMaozyManager: FragmentManager? = null

    private var mMaozyTempMaozyInputMaozyValue: String = ""
    private var mMaozyOnMaozyActionMaozyClickMaozyListener: (Int) -> Unit = {}

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        attrs?.let {
            context?.let { cxt ->
                val array = cxt.obtainStyledAttributes(attrs, R.styleable.BankAccountBill)

                cwbbaAddMode =
                    array.getBoolean(R.styleable.BankAccountBill_cwbbaAddMode, false)

                cwbbaIsSelect =
                    array.getBoolean(R.styleable.BankAccountBill_cwbbaIsSelect, true)

                cwbbaAccountTitle =
                    array.getString(R.styleable.BankAccountBill_cwbbaAccountTitle)

                cwbbaAddTitle =
                    array.getString(R.styleable.BankAccountBill_cwbbaAddTitle)

                array.recycle()
            }
        }
        context?.let {
            vMaozyRootMaozyView = View.inflate(it, R.layout.view_maozy_bill_bank_account, this)
            mMaozyViewMaozyBinding = ViewMaozyBillBankAccountBinding.bind(vMaozyRootMaozyView)
            initMaozySubviews()
        }
    }

    override fun initMaozySubviews() {
        try {
            if (cwbbaAddMode) {
                mMaozyViewMaozyBinding.imageBillBankAccountIcon.setImageResource(R.drawable.maozy_add_circle)
                mMaozyViewMaozyBinding.rlBillBankAccountContentBg.setBackgroundResource(R.drawable.maozy_bill_bank_account_add)
                mMaozyViewMaozyBinding.textBillBankAccountAdd.text = cwbbaAddTitle
                mMaozyViewMaozyBinding.textBillBankAccountTitle.visibility = GONE
                mMaozyViewMaozyBinding.textBillBankAccountNumber.visibility = GONE
                mMaozyViewMaozyBinding.textBillBankAccountAdd.visibility = VISIBLE
            } else {
                mMaozyViewMaozyBinding.imageBillBankAccountIcon.setImageResource(R.drawable.maozy_bank_account)
                mMaozyViewMaozyBinding.rlBillBankAccountContentBg.setBackgroundResource(R.drawable.maozy_bill_bank_account)
                mMaozyViewMaozyBinding.textBillBankAccountTitle.text = cwbbaAccountTitle
                mMaozyViewMaozyBinding.textBillBankAccountTitle.visibility = VISIBLE
                mMaozyViewMaozyBinding.textBillBankAccountNumber.visibility = VISIBLE
                mMaozyViewMaozyBinding.textBillBankAccountAdd.visibility = GONE
            }

            //优先级最高
            if (!cwbbaIsSelect) {
                if (!cwbbaAddMode) {
                    mMaozyViewMaozyBinding.rlBillBankAccountContentBg.setBackgroundResource(R.drawable.maozy_bill_bank_account_add)
                }
            }
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    override fun setMaozyFragmentMaozyManager(manager: FragmentManager) {
        mMaozySupportMaozyFragmentMaozyManager = manager
    }

    override fun setMaozyOnMaozyActionMaozyClickMaozyListener(listener: (Int) -> Unit) {
        mMaozyOnMaozyActionMaozyClickMaozyListener = listener
    }

    override fun setMaozyTitle(title: String) {

    }

    override fun setMaozyContent(content: String) {

    }

    override fun setMaozyHint(hint: String) {

    }

    override fun setMaozyDetail(detail: String) {

    }

    override fun setMaozyDetailMaozyColor(detailColor: Int) {

    }

    override fun setMaozyTitleMaozyColor(titleColor: Int) {

    }

    fun setMaozyAddMaozyMode(isAdd: Boolean) {
        cwbbaAddMode = isAdd
        initMaozySubviews()
    }

    fun setMaozyAccountMaozyNumber(account: String) {
        mMaozyViewMaozyBinding.textBillBankAccountNumber.text = (account)
    }

    fun setMaozyAccountMaozyTitle(accountTitle: String) {
        mMaozyViewMaozyBinding.textBillBankAccountTitle.text = (accountTitle)
    }

}